package net.oschina.app.emoji;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseRecyclerViewAdapter;
import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.util.UIUtils;

import java.util.List;

/**
 * Created by YL on 2017/4/7.
 */

public class EmojiBody extends Fragment {

    private RecyclerView mRecyclerView;
    private List<Emojicon> mAllByType;
    private MyAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.emoji_recycler, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.emoji_rec);
        mRecyclerView.setLayoutManager(new GridLayoutManager(UIUtils.getContext(), 7));
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAllByType = DisplayRules.getAllByType(0);
        initView();
        initEvent();
    }

    private void initEvent() {
        mAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                mOnEmojiClickLitener.onclick(mAllByType.get(position).getEmojiStr());
            }
        });
    }

    private void initView() {
        mAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mAdapter);
        
    }

    private class MyAdapter extends BaseRecyclerViewAdapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            imageView.setClickable(true);
            MyViewHolder myViewHolder = new MyViewHolder(imageView);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            MyViewHolder viewHolder = (MyViewHolder) holder;
            viewHolder.img.setImageResource(mAllByType.get(position).getResId());
        }

        @Override
        public int getItemCount() {
            return mAllByType.size();
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView;
        }
    }
    private OnEmojiClickLitener mOnEmojiClickLitener;

    public void setOnEmojiClickLitener(OnEmojiClickLitener onEmojiClickLitener) {
        mOnEmojiClickLitener = onEmojiClickLitener;
    }

    public interface OnEmojiClickLitener{
        void onclick(String str);
    }
}
